﻿using Mrsoft.Common;
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Diagnostics;
using MuYue.Common;
using MuYue.Http;

namespace Mrsoft.Core
{


    /// <summary>
    /// API响应函数
    /// </summary>
    public class APIJsonObject
    {
        public bool state { set; get; }

        public string message { set; get; }

        public string RegKey { set; get; }

        public string UserName { set; get; }

        public int Points { set; get; }

        public int PointsUsed { set; get; }
    }

    /// <summary>
    /// 答题基类
    /// 与Mrsoft的答题服务器交互
    /// </summary>
    public abstract class Dati
    {

        protected readonly string user;
        protected readonly string pass;

        protected Dati() {
            //初始化账号信息 并且 计算出用于网络验证的MD5加密后的密码
            user = new Config<string>(@".\common.cfg", "login/user", "").Value;
            pass = new Config<string>(@".\common.cfg", "login/pass", "").Value;
            pass = Utility.Md5Encoding(pass);
        }

        /// <summary>
        /// 扣除题分
        /// </summary>
        /// <param name="method"></param>
        /// <returns>扣分成功返回真 否则返回假并且提示</returns>
        protected bool LosePoint(string method = "answer") {
            APIJsonObject api;
            return CallWebAPI(method, out api) && api.state;
        }

        /// <summary>
        /// 查询题分
        /// </summary>
        /// <returns>返回账户剩余的题目</returns>
        protected int CheckPoint() {
            if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pass))
                return 0;
            APIJsonObject obj;
            if (CallWebAPI("check", out obj)) {
                return obj.Points;
            }
            return 0;
        }

        /// <summary>
        /// 请求答题
        /// </summary>
        /// <returns></returns>
        protected DatiResult AskDati() {
            try {
                APIJsonObject obj;
                if (CallWebAPI("answer", out obj)) {
                    if (!obj.state)
                        return new DatiResult(false, obj.message);
                    return new DatiResult(true, "ok");
                }
                else {
                    throw new Exception("答题扣分失败了!");
                }
            }
            catch (Exception ex) {
                return new DatiResult(false, ex.Message);
            }
        }

        public bool CallWebAPI(string method, out APIJsonObject obj) {
            using (Profiler.Step("答题联网")) {
                try {
                    var url = string.Format("{3}api?user={0}&pass={1}&method={2}&json=Y", user, pass, method, Mrsoft.Properties.Resources.website);
                    obj = WebEngine.JsonApi<APIJsonObject>(url);
                    return true;
                }
                catch (Exception ex) {
                    ShowMessage(ex.Message);
                    obj = null;
                    return false;
                }
            }
        }



        /// <summary>
        /// 数据回调给用户
        /// </summary>
        /// <param name="strMsg"></param>
        protected void ShowMessage(string strMsg) {
            MsgListener.CommonMsgShow("答题系统", strMsg);
        }

        /// <summary>
        /// 错误报告
        /// </summary>
        /// <returns>像服务器反馈错误答题</returns>
        protected bool ReportError() {
            return false;
        }

        /// <summary>
        /// 初始化答题COM
        /// </summary>
        /// <returns></returns>
        protected virtual bool InitCom() {
            return false;
        }
    }
}